//
//  ViewController.m
//  11-23date
//
//  Created by mac on 15-11-23.
//  Copyright (c) 2015年 ztt. All rights reserved.
//

#import "ViewController.h"
#import "inputViewController.h"
@interface ViewController ()<inputFinishDeleete,UITableViewDataSource>
{
    NSArray * data;
    NSArray * tels;
    NSIndexPath * curIndexpath;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"通讯录";
    data = @[
             @"尹彬",
             @"李梦杰",
             @"某某"
             ];
    tels = @[
             @"2135",
             @"2356",
             @"8565"
             ];
    NSUserDefaults * def = [NSUserDefaults standardUserDefaults];
    [def setObject:data forKey:@"data"];
    [def setObject:tels forKey:@"tels"];
    [def synchronize];
    
//    data = [def objectForKey:@"data"];
//    tels = [def objectForKey:@"tels"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.textLabel.text = data[indexPath.row];
    cell.detailTextLabel.text = tels[indexPath.row];
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    
//}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    inputViewController *con = [[inputViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    con.tel = cell.detailTextLabel.text;
    con.delegate = self;
    [self.navigationController pushViewController:con animated:YES];
    
    
    
    UIApplication * app = [UIApplication sharedApplication];
    NSString * tel = [NSString stringWithFormat:@"sms://%@",tels[indexPath.row]];
    [app openURL:[NSURL URLWithString:tel]];
}

- (void)inputFinished:(NSString *)text{
    NSLog(@"indexPath:%@",[self.tableView indexPathForSelectedRow]);
    NSMutableArray * arr = tels.mutableCopy;
    [arr replaceObjectAtIndex:curIndexpath.row withObject:text];
    tels = arr;
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
